﻿using BCC.Common;
using BCC.Common.AuthorizationUtils;
using BCC.Model.commodity;
using BCC.Model.ModelsOut;
using BCC.Model.user;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Security.Principal;
using System.Text;

namespace BCC.DAO.Commodity
{
    public static class CommodityDB
    {
        public static string serverAddress = ConfigurationManager.AppSettings["serverAddress"].Trim();
        /// <summary>
        /// 根据账号查询用户
        /// </summary>
        /// <param name="sendAccount"></param>
        /// <param name="receiveAccount"></param>
        /// <returns></returns>
        public static List<SCommodity> selCommodity(int pageNumber = 1, int pageSize = 20)
        {
            try
            {
                var url = serverAddress + $"api/Commodity/selCommodityAsync?pageNumber={pageNumber}&pageSize={pageSize}";
                var rsJson = AuthorizationUtils.HttpAuthorizationGet(url);

                var rs = JsonConvert.DeserializeObject<BaseOut>(rsJson);

                if (rs.code == "true" && rs.content != null)
                {
                    var commoditys = JsonConvert.DeserializeObject<List<SCommodity>>(rs.content.ToString());
                    return commoditys;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// 添加到购物车
        /// </summary>
        /// <param name="shoppingcart"></param>
        /// <returns></returns>
        public static bool selCommodity(SShoppingcartInput shoppingcart)
        {
            try
            {
                var url = serverAddress + $"api/Commodity/addShoppingcart";
                var rsJson = AuthorizationUtils.HttpAuthorizationPost(url, shoppingcart);

                var rs = JsonConvert.DeserializeObject<BaseOut>(rsJson);

                if (rs.code == "true")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
